<script>

$(function(){
	$('.admin-setting,.site-setting,.mobile-setting').unbind('click').click(function(){
		var obj = this;
		if($(this).hasClass('disabled')){
			return false;
		}
		var postdata = {};var btn_selector = '';
		if($(this).data('type')=='Site'){
			postdata = {'setting[Site][style]':$(this).data('styleid')};
			btn_selector = '.site-setting';
			
		}
		else if($(this).data('type')=='Admin'){
			postdata = {'setting[Admin][style]':$(this).data('styleid')};
			btn_selector = '.admin-setting';
		}
		else if($(this).data('type')=='Mobile'){
			postdata = {'setting[Site][mobile_style]':$(this).data('styleid')};
			btn_selector = '.mobile-setting';
		}
		$.ajax({
			async:true,
			type:'post',
			success:function(request, xhr) {				
				//showDialogMessage(request);
				$(btn_selector).removeClass('disabled').addClass('btn-primary').html('启用此风格');
				$(obj).removeClass('btn-primary').addClass('disabled').html('<i class="glyphicon glyphicon-ok"></i>已启用');
			},
			url:ADMIN_BASEURL+'/admin/settings/ajaxesave',
			data:postdata,
			dataType:'json'
		}) ;
		return false;
	})
})

</script>
<div class="languages index">
	<div>
         <?php echo $this->Html->link(__('New style'), array('controller'=>'styles','action'=>'addstyle'),array('class'=>'btn btn-primary')); ?>
         
    </div>
    
    <div class="well mt10">
    	只能启用一个风格，不支持同时启用多种风格。
    	启用新风格时，旧风格将失效。
    </div>

    <table id="style_setting" class="table table-striped table-hover">
    <?php
        $tableHeaders =  $this->Html->tableHeaders(array(
            '风格',
            '标识符',
			'前台PC页风格',
			'前台手机页风格',
			'管理后台风格',
            __('Actions'),
        ),array('class'=>'info'));

        echo '<thead>'.$tableHeaders.'</thead>';

        $rows = array();
        foreach ($styles AS $style) {
            $actions = ' ' . $this->Html->link(__('Edit'), array('controller'=>'stylevars','action' => 'edit', $style['Style']['id']));
			$actions .= ' ' . $this->Html->link(__('Preview'), '/../?styleid='.$style['Style']['id'],array('target'=>'_blank'));
            $actions .= ' ' . $this->Html->link(__('Delete'), array('controller'=>'styles','action' => 'delete', $style['Style']['id']),array('class'=>'delete'), __('Are you sure?', true));
			if($site_style==$style['Style']['id']){
				$site_active = '<button data-type="Site" data-styleid="'.$style['Style']['id'].'" type="button" class="site-setting btn btn-small disabled"><i class="glyphicon glyphicon-ok"></i>已启用</button>';
			}
			else{
				$site_active = '<button data-type="Site" data-styleid="'.$style['Style']['id'].'" type="button" class="site-setting btn btn-small btn-primary">启用此风格</button>';
			}
			if($admin_style_id==$style['Style']['id']){
				$admin_active = '<button data-type="Admin" data-styleid="'.$style['Style']['id'].'" type="button" class="admin-setting btn btn-small disabled"><i class="glyphicon glyphicon-ok"></i>已启用</button>';
			}
			else{
				$admin_active = '<button data-type="Admin" data-styleid="'.$style['Style']['id'].'" type="button" class="admin-setting btn btn-small btn-primary">启用此风格</button>';
			}

			if($mobile_style==$style['Style']['id']){
				$mobile_active = '<button data-type="Mobile" data-styleid="'.$style['Style']['id'].'" type="button" class="mobile-setting btn btn-small disabled"><i class="glyphicon glyphicon-ok"></i>已启用</button>';
			}
			else{
				$mobile_active = '<button data-type="Mobile" data-styleid="'.$style['Style']['id'].'" type="button" class="mobile-setting btn btn-small btn-primary">启用此风格</button>';
			}

            $rows[] = array(
                $style['Style']['name'],
                $style['Style']['slug'],
				$site_active,
				$mobile_active,
				$admin_active,
                $actions,
            );
        }
        echo $this->Html->tableCells($rows);
    ?>
    </table>
</div>
<script>
$(function(){
	$('.delete').click(function(){
		ajaxAction(this.href);
		return false;
	});
})
</script>